<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dates</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="tips.html" title="Chapter 21. Tips &amp; Tricks">
<link rel="prev" href="tips.passing.vars.html" title="Passing variable title to header template">
<link rel="next" href="tips.wap.html" title="WAP/WML">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Dates</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="tips.passing.vars.html">Prev</a> </td>
<th width="60%" align="center">Chapter 21. Tips &amp; Tricks</th>
<td width="20%" align="right"> <a accesskey="n" href="tips.wap.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Dates">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="tips.dates"></a>Dates</h2></div></div></div>
<p>
    As a rule of thumb, always pass dates to Smarty as
    <a class="ulink" href="http://php.net/time" target="_top">timestamps</a>. This
    allows template designers to use the <a class="link" href="language.modifier.date.format.html" title="date_format"><code class="varname">date_format</code>
    </a> modifier for full control over date formatting, and also makes it
    easy to compare dates if necessary.
   </p>
<div class="example">
<a name="id468891"></a><p class="title"><b>Example 21.4. Using date_format</b></p>
<div class="example-contents">
<pre class="programlisting">

{$startDate|date_format}

    </pre>
<p>
     This will output:
    </p>
<pre class="screen">

Jan 4, 2009

    </pre>
<pre class="programlisting">

{$startDate|date_format:"%Y/%m/%d"}

    </pre>
<p>
     This will output:
    </p>
<pre class="screen">

2009/01/04

    </pre>
<p>
     Dates can be compared in the template by timestamps with:
    </p>
<pre class="programlisting">

{if $order_date &lt; $invoice_date}
   ...do something..
{/if}

    </pre>
</div>
</div>
<br class="example-break"><p>
    When using <a class="link" href="language.function.html.select.date.html" title="{html_select_date}">
    <code class="varname">{html_select_date}</code></a> in a template, the programmer
    will most likely want to convert the output from the form back into
     timestamp format. Here is a function to help you with that.
   </p>
<div class="example">
<a name="id468954"></a><p class="title"><b>Example 21.5. Converting form date elements back to a timestamp</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;?php

// this assumes your form elements are named
// startDate_Day, startDate_Month, startDate_Year

$startDate = makeTimeStamp($startDate_Year, $startDate_Month, $startDate_Day);

function makeTimeStamp($year='', $month='', $day='')
{
   if(empty($year)) {
       $year = strftime('%Y');
   }
   if(empty($month)) {
       $month = strftime('%m');
   }
   if(empty($day)) {
       $day = strftime('%d');
   }

   return mktime(0, 0, 0, $month, $day, $year);
}
?&gt;

    </pre></div>
</div>
<br class="example-break"><p>
   See also
   <a class="link" href="language.function.html.select.date.html" title="{html_select_date}">
   <code class="varname">{html_select_date}</code></a>,
   <a class="link" href="language.function.html.select.time.html" title="{html_select_time}">
   <code class="varname">{html_select_time}</code></a>,
   <a class="link" href="language.modifier.date.format.html" title="date_format">
   <code class="varname">date_format</code></a>
   and <a class="link" href="language.variables.smarty.html#language.variables.smarty.now" title="{$smarty.now}">
   <em class="parameter"><code>$smarty.now</code></em></a>,
   </p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="tips.passing.vars.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="tips.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="tips.wap.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Passing variable title to header template </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> WAP/WML</td>
</tr>
</table>
</div>
</body>
</html>
